package com.example.zsr.downloadcontinue;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.SeekBar;

import com.example.zsr.downloadcontinue.ToolUtils.FileInfo;

public class MainActivity extends AppCompatActivity {

    private FileInfo mFileInfo;

    private String fileDir;

    public static final String FILEDIR = Environment
            .getExternalStorageDirectory().getAbsolutePath() + "/downloads";

    private SeekBar mSeekBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        fileDir = getFilesDir()+"/mydownload/";
        mFileInfo = new FileInfo(0,"http://www.imooc.com/mobile/mukewang.apk",
                fileDir,"north.apk",0,null,0);
        mSeekBar = (SeekBar) findViewById(R.id.seekBar);
        mSeekBar.setMax(100);
        IntentFilter filter = new IntentFilter();
        filter.addAction("com.msg.apk_install_progress");
        registerReceiver(broadcastReceiver,filter);
    }

    BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            int progress = intent.getIntExtra("progress",0);
            mSeekBar.setProgress(progress);
        }
    };

    public void start(View view){
        Intent start = new Intent(this,DowaloadService.class);
        start.putExtra("fileinfo",mFileInfo);
        start.putExtra("service",1);
        startService(start);
    }

    public void stop(View view){
        Intent start = new Intent(this,DowaloadService.class);
        start.putExtra("fileinfo",mFileInfo);
        start.putExtra("service",0);
        startService(start);
    }

}
